# 11/14 correct simX effects, score test possible controls as first step 
# 11/9 models for submission (fingers crossed); add immgen and gender to race fn; no selection endow
# run a sequence of models

library(RSiena)
library(parallel)

#rm(list=ls())

#Set working directory
setwd('/Users/drschaef/Google Drive/Projects/Adriana/Papers/Homophily/Output')
setwd('/Users/drschaef/My Drive/Projects/Adriana/Papers/Homophily/Output')

mydatRF <-  readRDS("rfnetM123_221014.rds") # friendships and race only

myeffBase <- getEffects( mydatRF, nintn=60 )   # race and friendship network
# base effects that will be in all models
# friendship effects
myeffBase <- includeTimeDummy(myeffBase, density, name='friendship', timeDummy = "all")
myeffBase <- includeEffects(myeffBase, recip, name="friendship")
myeffBase <- includeEffects(myeffBase, outActSqrt, name="friendship")
myeffBase <- includeEffects(myeffBase, inPopSqrt, name="friendship")
myeffBase <- includeEffects(myeffBase, outPopSqrt, name="friendship")
myeffBase <- includeEffects(myeffBase, transTrip, name="friendship")
myeffBase <- includeEffects(myeffBase, nbrDist2, name="friendship")
myeffBase <- includeEffects(myeffBase, transRecTrip, name="friendship")
myeffBase <- includeEffects(myeffBase, sameX, name="friendship", interaction1="female")
myeffBase <- includeEffects(myeffBase, sameX, name="friendship", interaction1="gradecohort")
myeffBase <- includeEffects(myeffBase, X, name="friendship", interaction1="ecaDum")

# race function effects
myeffBase <- setEffect(myeffBase, RateX, type='rate', name="raceNet", interaction1="mono", fix=T, initialValue=-100) 
myeffBase <- includeTimeDummy(myeffBase, density, name="raceNet", timeDummy = "all")
myeffBase <- includeEffects(myeffBase, altX, name="raceNet", interaction1="black") 
myeffBase <- includeEffects(myeffBase, altX, name="raceNet", interaction1="natam") 
myeffBase <- includeEffects(myeffBase, altX, name="raceNet", interaction1="latin") 
myeffBase <- includeEffects(myeffBase, altX, name="raceNet", interaction1="asian") 
myeffBase <- includeEffects(myeffBase, altX, name="raceNet", interaction1="orace") 
myeffBase <- includeEffects(myeffBase, altX, name="raceNet", interaction1="mrace") 
myeffBase <- includeInteraction(myeffBase, egoX, altX, name="raceNet", interaction1=c('perla','black')) 
myeffBase <- includeInteraction(myeffBase, egoX, altX, name="raceNet", interaction1=c('perla','latin')) 
myeffBase <- includeInteraction(myeffBase, egoX, altX, name="raceNet", interaction1=c('perla','asian')) 
myeffBase <- includeInteraction(myeffBase, egoX, altX, name="raceNet", interaction1=c('perla','orace')) 
myeffBase <- includeInteraction(myeffBase, egoX, altX, name="raceNet", interaction1=c('perla','mrace')) 
myeffBase <- includeEffects(myeffBase, egoX, name="raceNet", interaction1='perla') 
myeffBase <- includeEffects(myeffBase, simEgoInDist2, name="raceNet", interaction1='perla') 

# get starting values
setwd('/Users/drschaef/Google Drive/Projects/Adriana/Papers/Homophily/Output/tempM')
setwd('/Users/drschaef/My Drive/Projects/Adriana/Papers/Homophily/Output/tempM')
stVal <- readRDS('fullModelM_230715.RDS')
myeffBase <- updateTheta(myeffBase, stVal)

# score tests
myeffScore <- myeffBase
# friendship function
myeffScore <- setEffect(myeffScore, simX, name="friendship", interaction1="pared8", include=T, test=T, fix=T)
myeffScore <- setEffect(myeffScore, sameX, name="friendship", interaction1="immgen", include=T, test=T, fix=T)
myeffScore <- setEffect(myeffScore, simX, name="friendship", interaction1="acad9", include=T, test=T, fix=T)
myeffScore <- setEffect(myeffScore, simX, name="friendship", interaction1="disc", include=T, test=T, fix=T)
myeffScore <- setEffect(myeffScore, egoX, name="friendship", interaction1="pared8", include=T, test=T, fix=T)
myeffScore <- setEffect(myeffScore, egoX, name="friendship", interaction1="immgen", include=T, test=T, fix=T)
myeffScore <- setEffect(myeffScore, egoX, name="friendship", interaction1="acad9", include=T, test=T, fix=T)
myeffScore <- setEffect(myeffScore, egoX, name="friendship", interaction1="disc", include=T, test=T, fix=T)
myeffScore <- setEffect(myeffScore, egoX, name="friendship", interaction1="female", include=T, test=T, fix=T)
myeffScore <- setEffect(myeffScore, egoX, name="friendship", interaction1="gradecohort", include=T, test=T, fix=T)
myeffScore <- setEffect(myeffScore, altX, name="friendship", interaction1="pared8", include=T, test=T, fix=T)
myeffScore <- setEffect(myeffScore, altX, name="friendship", interaction1="immgen", include=T, test=T, fix=T)
myeffScore <- setEffect(myeffScore, altX, name="friendship", interaction1="acad9", include=T, test=T, fix=T)
myeffScore <- setEffect(myeffScore, altX, name="friendship", interaction1="disc", include=T, test=T, fix=T)
myeffScore <- setEffect(myeffScore, altX, name="friendship", interaction1="female", include=T, test=T, fix=T)
myeffScore <- setEffect(myeffScore, altX, name="friendship", interaction1="gradecohort", include=T, test=T, fix=T)

# race function
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('disc','black'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('disc','latin'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('disc','asian'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('disc','orace'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('disc','mrace'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('immgen','black'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('immgen','latin'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('immgen','asian'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('immgen','orace'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('immgen','mrace'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('female','black'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('female','latin'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('female','asian'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('female','orace'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('female','mrace'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('acad9','black'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('acad9','latin'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('acad9','asian'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('acad9','orace'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('acad9','mrace'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('pared8','black'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('pared8','latin'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('pared8','asian'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('pared8','orace'), include=T, test=T, fix=T) 
myeffScore <- includeInteraction(myeffScore, egoX, altX, name="raceNet", interaction1=c('pared8','mrace'), include=T, test=T, fix=T) 
myeffScore <- includeEffects(myeffScore, egoX, name="raceNet", interaction1='disc', include=T, test=T, fix=T) 
myeffScore <- includeEffects(myeffScore, egoX, name="raceNet", interaction1='immgen', include=T, test=T, fix=T) 
myeffScore <- includeEffects(myeffScore, egoX, name="raceNet", interaction1='female', include=T, test=T, fix=T) 
myeffScore <- includeEffects(myeffScore, egoX, name="raceNet", interaction1='acad9', include=T, test=T, fix=T) 
myeffScore <- includeEffects(myeffScore, egoX, name="raceNet", interaction1='pared8', include=T, test=T, fix=T) 
myeffScore <- includeEffects(myeffScore, simEgoInDist2, name="raceNet", interaction1='disc', include=T, test=T, fix=T) 
myeffScore <- includeEffects(myeffScore, simEgoInDist2, name="raceNet", interaction1='immgen', include=T, test=T, fix=T) 
myeffScore <- includeEffects(myeffScore, simEgoInDist2, name="raceNet", interaction1='female', include=T, test=T, fix=T) 
myeffScore <- includeEffects(myeffScore, simEgoInDist2, name="raceNet", interaction1='acad9', include=T, test=T, fix=T) 
myeffScore <- includeEffects(myeffScore, simEgoInDist2, name="raceNet", interaction1='pared8', include=T, test=T, fix=T) 

##define the algorithm settings
myAlgorithm <- sienaAlgorithmCreate( projname = 'model231114_M', nsub=4, n3=2000, cond=T, condname='friendship')

m0.1 <- siena07( myAlgorithm, data = mydatRF, effects = myeffScore, useCluster=T, initC=T, nbrNodes=7, returnDeps=F)
summary(m0.1)

# M1: a preliminary "final" model
myeff1 <- myeffBase
# add effects with significant score tests
#myeff1 <- setEffect(myeff1, altX, name="friendship", interaction1="gradecohort",include=T, fix=F, test=F)
myeff1 <- setEffect(myeff1, egoX, name="friendship", interaction1="gradecohort",include=T, fix=F, test=F)
myeff1 <- setEffect(myeff1, egoX, name="friendship", interaction1="pared8",include=T, fix=F, test=F)
myeff1 <- setEffect(myeff1, simX, name="friendship", interaction1="pared8",include=T, fix=F, test=F)
myeff1 <- setEffect(myeff1, sameX, name="friendship", interaction1="immgen",include=T, fix=F, test=F)
myeff1 <- setEffect(myeff1, egoX, name="friendship", interaction1="acad9",include=T, fix=F, test=F)
myeff1 <- setEffect(myeff1, simX, name="friendship", interaction1="acad9",include=T, fix=F, test=F)
myeff1 <- setEffect(myeff1, simX, name="friendship", interaction1="disc",include=T, fix=F, test=F)
myeff1 <- includeEffects(myeff1, egoX, name="raceNet", interaction1='disc',include=T, fix=F, test=F) 
myeff1 <- includeInteraction(myeff1, egoX, altX, name="raceNet", interaction1=c('disc','black'),include=T, fix=F, test=F) 
myeff1 <- includeInteraction(myeff1, egoX, altX, name="raceNet", interaction1=c('immgen','asian'),include=T, fix=F, test=F) 
myeff1 <- includeInteraction(myeff1, egoX, altX, name="raceNet", interaction1=c('immgen','orace'),include=T, fix=F, test=F) 

# add race effects
myeff1 <- includeEffects(myeff1, X, name="friendship", interaction1="mumu")
myeff1 <- includeEffects(myeff1, X, name="friendship", interaction1="mumo")
myeff1 <- includeEffects(myeff1, X, name="friendship", interaction1="momu")
myeff1 <- includeEffects(myeff1, from, name="friendship", interaction1="raceNet")
myeff1 <- setEffect(myeff1, to, name="raceNet", interaction1="friendship")  # friend influence

#m0.2 <- siena07( myAlgorithm, data = mydatRF, effects = myeff1, useCluster=T, initC=T, nbrNodes=7, prevAns=m0.1, returnDeps=F)
#summary(m0.2)

###############
# drop any n.s. effects from myeff1 to create final model
# - comment out above and recreate myeff1 above
# - comment out m0.2
# run m1 and m2
###############

# M2: separate influence into create and endow
myeff2 <- myeff1
myeff2 <- setEffect(myeff2, to, name="raceNet", interaction1="friendship", type='creation', include=T, fix=F, test=F)
myeff2 <- setEffect(myeff2, to, name="raceNet", interaction1="friendship", type='endow', include=T, fix=F, test=F)
myeff2 <- setEffect(myeff2, to, name="raceNet", interaction1="friendship", type='eval', include=F, fix=F, test=F)


##define the algorithm settings - increase to 5k p3 iterations
myAlgorithm <- sienaAlgorithmCreate( projname = 'model231114_M', nsub=4, n3=5000, cond=T, condname='friendship')


# run the models
m1 <- siena07( myAlgorithm, data = mydatRF, effects = myeff1, useCluster=T, initC=T, nbrNodes=7, prevAns=stVal, returnDeps=T)
m2 <- siena07( myAlgorithm, data = mydatRF, effects = myeff2, useCluster=T, initC=T, nbrNodes=7, prevAns=m1, returnDeps=T)


saveRDS(myeff1, 'myeff1_M_231114.RDS')   
saveRDS(m1, 'model1_M_231114.RDS')
saveRDS(myeff2, 'myeff2_M_231114.RDS')   
saveRDS(m2, 'model2_M_231114.RDS')

# test the interaction between mumu and racial identity homophily
setwd('/Users/drschaef/Google Drive/Projects/Adriana/Papers/Homophily/Output')
setwd('/Users/drschaef/My Drive/Projects/Adriana/Papers/Homophily/Output')
mydatRF <-  readRDS("rfnetM123_221014.rds") # friendships and race only
myAlgorithm <- sienaAlgorithmCreate( projname = 'model231114_mumuFollowup_M_S', nsub=4, n3=5000, cond=T, condname='friendship')
myeff1 <- readRDS('tempM/myeff1_M_231114.RDS')   # effects object saved from M1
myeff1 <- includeInteraction(myeff1, X, from, name='friendship', interaction1=c('mumu','raceNet'))
stVal <- readRDS('tempM/model1_M_231114.RDS')
m1a <- siena07( myAlgorithm, data = mydatRF, effects = myeff1, useCluster=T, initC=T, nbrNodes=7, prevAns=stVal, returnDeps=T)
m1a

# 3. followups to test for multi moderation of current race homophily
setwd('/Users/drschaef/Google Drive/Projects/Adriana/Papers/Homophily/Output')
setwd('/Users/drschaef/My Drive/Projects/Adriana/Papers/Homophily/Output')
mydatRF <-  readRDS("rfnetM123_221014.rds") # friendships and race only
myAlgorithm <- sienaAlgorithmCreate( projname = 'model231114_M', nsub=4, n3=5000, cond=T, condname='friendship')
myeff <- readRDS('tempM/myeff2_M_231114.RDS')   # effects object saved from M2
stVal <- readRDS('tempM/model2_M_231114.RDS')
myeff4 <- myeff
myeff4 <- includeEffects(myeff4, egoX, name="friendship", interaction1='multi')
myeff4 <- includeInteraction(myeff4, egoX, from, name="friendship", interaction1=c('multi','raceNet')) 

setwd('/Users/drschaef/Google Drive/Projects/Adriana/Papers/Homophily/Output/tempM')
setwd('/Users/drschaef/My Drive/Projects/Adriana/Papers/Homophily/Output/tempM')
m4 <- siena07( myAlgorithm, data = mydatRF, effects = myeff4, useCluster=T, initC=T, nbrNodes=7, prevAns=stVal)

myeff3 <- myeff
myeff3 <- includeEffects(myeff3, egoX, name="friendship", interaction1='multi')
myeff3 <- includeEffects(myeff3, X, name="friendship", interaction1="mumu", include=F, fix=F, test=F)
myeff3 <- includeEffects(myeff3, X, name="friendship", interaction1="mumo", include=F, fix=F, test=F)
myeff3 <- includeEffects(myeff3, X, name="friendship", interaction1="momu", include=F, fix=F, test=F)
myeff3 <- includeInteraction(myeff3, egoX, from, name="friendship", interaction1=c('multi','raceNet')) 
m3 <- siena07( myAlgorithm, data = mydatRF, effects = myeff3, useCluster=T, initC=T, nbrNodes=7, prevAns= stVal)

myeff5 <- myeff
myeff5 <- includeEffects(myeff5, egoX, name="friendship", interaction1='multi')
myeff5 <- includeInteraction(myeff5, X, from, name="friendship", interaction1=c('mumu','raceNet')) 
myeff5 <- includeInteraction(myeff5, egoX, from, name="friendship", interaction1=c('multi','raceNet')) 
m5 <- siena07( myAlgorithm, data = mydatRF, effects = myeff5, useCluster=T, initC=T, nbrNodes=7, prevAns= stVal)


